"""
Case Type   : cm_install
Case Name   : cm_install执行前xml文件配置，cm的IP配置和om配置不一致
Create At   : 2025/4/22
Owner       : @ningyali
Description :
    1.配置xml文件中的cm相关参数，cm的IP配置和om配置不一致
    2.数据库用户下执行cm_install
    3.cm_ctl查看集群状态
    4.gs_om查看集群状态
    5.查看数据库用户下进程和定时任务
Expect      :
    1.执行成功
    2.安装失败
    3.失败,未安装cm
    4.未安装cm
    5.无cm_server,有om_monitor、cm_agent，定时任务
History     :
"""
import os
import unittest

from testcase.utils.ComInstall import CmInstall
from testcase.utils.ComInstall import CmUnInstall
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class CMInstallCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.root_nodes = (Node('PrimaryRoot'), Node('Standby1Root'),
                           Node('Standby2Root'))
        self.user_nodes = (Node('PrimaryDbUser'), Node('Standby1DbUser'),
                           Node('Standby2DbUser'))
        self.constant = Constant()
        self.install_pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.cm = CmInstall(self.install_pkg_path, self.pri_sh.node.ssh_user)
        self.ips = ','.join([n.ssh_host for n in self.user_nodes])
        self.err_ips = '10.10.10.10,10.10.10.11,10.10.10.12'

    def test_install(self):
        text = '----step1:配置xml文件中的cm相关参数，cm的IP配置和om配置不一致;expect:执行成功----'
        self.log.info(text)
        res = self.cm.update_xml_for_cm(*self.root_nodes)
        sed_cmd = f"grep -n \"cmServerListenIp1\" {self.cm.xml_path}|" \
                  f"awk -F \":\" '{{{{print $1}}}}'|" \
                  f"xargs -I {{{{}}}} sed -i '{{{{}}}} " \
                  f"s#{self.ips}#{self.err_ips}#g' {self.cm.xml_path};"
        sed_res = self.common.get_sh_result(self.user_nodes[0], sed_cmd)
        self.assertGreater(res.count('cmDir'), 1, f'执行失败: {text}')
        self.assertNotIn('error', sed_res.lower(), f'执行失败: {text}')

        text = '----step2:数据库用户下执行cm_install;expect:安装失败----'
        self.log.info(text)
        install_res = self.cm.cm_install(self.user_nodes[0])
        expect1 = f"[GAUSS-51220] : The IP address " \
                  f"['{self.err_ips.split(',')[0]}'] with cm_server " \
                  f"is incorrect"
        expect2 = f"The IP address must be the same as the cm_agent " \
                  f"['{self.user_nodes[0].ssh_host}']."
        self.assertIn(expect1, install_res, '执行失败:' + text)
        self.assertIn(expect2, install_res, '执行失败:' + text)

        text = '----step3:cm_ctl查看集群状态;expect:失败,未安装cm----'
        self.log.info(text)
        show_status = f'source {macro.DB_ENV_PATH};' \
                      f'cm_ctl query -Civdp'
        status_msg = self.common.get_sh_result(self.user_nodes[0], show_status)
        self.assertIn("cm_ctl: can't connect to cm_server", status_msg,
                      "执行失败:" + text)

        text = '----step4:gs_om查看集群状态;expect:未安装cm----'
        self.log.info(text)
        status_msg = self.pri_sh.get_db_cluster_status('detail')
        self.log.info(status_msg)
        self.assertNotIn("CMServer State", status_msg, "执行失败:" + text)

        text = '----step5:查看数据库用户下进程和定时任务;' \
               'expect:无cm_server,有om_monitor、cm_agent，定时任务----'
        self.log.info(text)
        show_cmd = "crontab -l;ps ux|egrep 'cm_server|cm_agent|om_monitor';"
        process_res = self.pri_sh.exec_gs_ssh(show_cmd)
        self.assertNotIn('bin/cm_server', process_res, "执行失败:" + text)
        self.assertNotIn('bin/cm_agent', process_res, "执行失败:" + text)
        self.assertNotIn('om_monitor -L', process_res, "执行失败:" + text)

    def tearDown(self):
        text = '----poststep1:还原xml;expect:执行成功----'
        self.log.info(text)
        sed_cmd = f'sed -i \'/cm/d\' {self.cm.xml_path};'
        sed_res = self.common.get_sh_result(self.user_nodes[0], sed_cmd)
        self.assertNotIn('error', sed_res.lower(), "执行失败:" + text)
        self.log.info(f'----{os.path.basename(__file__)} end----')
